package com.course.smart_campus.controller;

import com.course.smart_campus.entity.DeliverOrder;
import com.course.smart_campus.entity.Result;
import com.course.smart_campus.service.DeliverOrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/deliverOrder")
public class deliverOrderController {
    @Resource
    private DeliverOrderService deliverOrderService;
    /**
     * 创建委托
     */
    @PostMapping("/{clientId}")
    public Result createDeliverOrder(@RequestBody DeliverOrder deliverOrder, @PathVariable("clientId") Integer clientId) {
        deliverOrder.setClientId(clientId);
        deliverOrder.setStatus(0);
        boolean flag = deliverOrderService.save(deliverOrder);
        if (flag){
            return new Result(true, "创建成功");
        }else {
            return new Result(false, "创建失败");
        }
    }
    /**
     * 发布人查询委托
     */
    @GetMapping("/clientFind/{clientId}")
    public Result ClientFindDeliverOrder(@PathVariable("clientId") int clientId) {
        List<DeliverOrder> deliverOrderList = deliverOrderService.getByClientId(clientId);
        if (deliverOrderList.size() > 0){
            return new Result(true, "查询成功", deliverOrderList);
        }else {
            return new Result(false, "查询失败");
        }
    }
    /**
     * 发布人查询委托
     */
    @GetMapping("/acceptFind/{accetperId}")
    public Result AcceptFindDeliverOrder(@PathVariable("accetperId") int accepterId) {
        List<DeliverOrder> deliverOrderList = deliverOrderService.getByAcceptId(accepterId);
        if (deliverOrderList.size() > 0){
            return new Result(true, "查询成功", deliverOrderList);
        }else {
            return new Result(false, "查询失败");
        }
    }
    /**
     * 删除委托
     */
    @DeleteMapping("/{id}")
    public Result deleteDeliverOrder(@PathVariable("id") int id) {
        boolean flag = deliverOrderService.removeById(id);
        if (flag){
            return new Result(true, "删除成功");
        }else {
            return new Result(false, "删除失败");
        }
    }
    /**
     * 修改委托信息,委托id
     */
    @PutMapping("/{id}")
    public Result updateDeliverOrder(@RequestBody DeliverOrder deliverOrder, @PathVariable("id") Integer id) {
        deliverOrder.setId(id);
        boolean flag = deliverOrderService.updateById(deliverOrder);
        if (flag){
            return new Result(true, "修改成功");
        }else {
            return new Result(false, "修改失败");
        }
    }

    /**
     * 修改委托状态
     */
    @PutMapping("/updateStatus/{id}")
    public Result updateStatus(@PathVariable("id") int id,@RequestParam int status) {
        boolean flag = deliverOrderService.updateStatus(id,status);
        if (flag){
            return new Result(true, "修改成功");
        }else {
            return new Result(false, "修改失败");
        }
    }

    /**
     * 接受委托
     */
    @PutMapping("/accept/{acceptId}/{orderId}")
    public Result accept( @PathVariable("acceptId") int acceptId,@PathVariable("orderId") int id) {
        boolean flag = deliverOrderService.accept(acceptId,id);
        if (flag){
            return new Result(true, "接收委托成功");
        }else {
            return new Result(false, "接收委托失败");
        }
    }
    /**
     * 取消接收委托
     */
    @PutMapping("/cancelAccept/{acceptId}/{orderId}")
    public Result cancelAccept( @PathVariable("acceptId") int acceptId,@PathVariable("orderId") int id) {
        boolean flag = deliverOrderService.cancelAccept(acceptId,id);
        if (flag){
            return new Result(true, "取消接收委托成功");
        }else {
            return new Result(false, "取消接收委托失败");
        }
    }
    /**
     * 完成委托
     */
    @PutMapping("/finish/{id}")
    public Result finish(@PathVariable("id") int id) {
        boolean flag = deliverOrderService.finish(id);
        if (flag){
            return new Result(true, "完成委托成功");
        }else {
            return new Result(false, "完成委托失败");
        }
    }
    /**
     * 根据id查询委托，可以不写
     */
    @GetMapping("/{id}")
    public Result getDeliverOrder(@PathVariable("id") int id) {
        DeliverOrder deliverOrder = deliverOrderService.getById(id);
        if (deliverOrder != null){
            return new Result(true, "查询成功", deliverOrder);
        }else {
            return new Result(false, "查询失败");
        }
    }


}
